import { getRoleTypes } from '@/api/common'
import { dictDataSubmit, getDict } from '@/api/dict'

export const state = {
  dict: {
    yesNo: [
      { value: '1', label: '是' },
      { value: '2', label: '否' }
    ],
    classCategory: [
      { value: '1', label: '托' },
      { value: '2', label: '早' }
    ],
    courseCategory: [
      { type: '3', category: '1', label: '系统课' },
      { type: '2', category: '3', label: '自建课' },
      // { type: '1', category: '5', label: 'QZL课程' },
      { type: '0', category: '0', label: '活动课' }
    ],
    sex: [
      { value: '1', label: '男' },
      { value: '0', label: '女' },
      { value: '2', label: '未知' }
    ],
    bloodType: [
      { value: 'A', label: 'A型血' },
      { value: 'B', label: 'B型血' },
      { value: 'AB', label: 'AB型血' },
      { value: 'O', label: 'O型血' }
    ],
    relationships: [
      { value: '妈妈', label: '妈妈' },
      { value: '爸爸', label: '爸爸' },
      { value: '爷爷', label: '爷爷' },
      { value: '奶奶', label: '奶奶' },
      { value: '外公', label: '外公' },
      { value: '外婆', label: '外婆' },
      { value: '其他', label: '其他' }
    ],
    contactOrders: [
      { value: '第一顺位', label: '第一顺位' },
      { value: '第二顺位', label: '第二顺位' },
      { value: '第三顺位', label: '第三顺位' },
      { value: '其他', label: '其他' }
    ],
    educations: [
      { value: '中专', label: '中专' },
      { value: '大专', label: '大专' },
      { value: '本科', label: '本科' },
      { value: '研究生', label: '研究生' },
      { value: '硕士以上', label: '硕士以上' },
      { value: '其他', label: '其他' }
    ],
    noticeType: [
      { value: '1', label: '普通通知' },
      { value: '2', label: '活动通知' },
      { value: '3', label: '会议通知' }
    ],
    payChannel: [
      { id: '1', name: '现金' },
      { id: '2', name: '刷卡' },
      { id: '3', name: '银行转账' },
      { id: '4', name: '支付宝' },
      { id: '5', name: '微信' },
      { id: '6', name: '其他' }
    ],
    clienteleType: [
      { id: 'A', name: 'A类：有支付能力也有消费意愿' },
      { id: 'B', name: 'B类：有支付能力消费意愿一般' },
      { id: 'C', name: 'C类：支付能力受限消费意愿强烈' },
      { id: 'D', name: 'D类：无支付能力' }
    ],
    clueFollowTypes: [
      { id: '1', name: '电话' },
      { id: '2', name: '微信' },
      { id: '3', name: '邮件' },
      { id: '4', name: '短信' },
      { id: '5', name: '拜访' },
      { id: '6', name: 'QQ' },
      { id: '7', name: '其他' }
    ],
    clueStatus: [
      { id: '1', name: '未处理' },
      { id: '2', name: '初访' },
      { id: '3', name: '已邀约' },
      { id: '4', name: '已到访' },
      { id: '5', name: '不感兴趣' },
      { id: '6', name: '未接通' },
      { id: '7', name: '地域不符' },
      { id: '8', name: '孩子超龄' },
      { id: '9', name: '无效的联系方式' }
    ],
    clientStatus: [
      { id: '1', name: '未处理', type: 'warning' },
      { id: '2', name: '已预约', type: 'primary' },
      { id: '3', name: '已到访', type: 'primary' },
      { id: '4', name: '已完成申请表', type: 'primary' },
      { id: '5', name: '已面试', type: 'primary' },
      { id: '6', name: '已交定金', type: 'primary' },
      { id: '7', name: '面试不通过', type: 'danger' },
      { id: '8', name: '方案不满意', type: 'danger' },
      { id: '9', name: '园区不满意', type: 'danger' },
      { id: '10', name: '无支付能力', type: 'danger' },
      { id: '11', name: '已成交', disabled: true, type: 'success' },
      { id: '12', name: '其他', type: 'info' }
    ],
    contractStatus: [
      { id: '0', name: '未开始', type: 'warning' },
      { id: '1', name: '进行中', type: 'primary' },
      { id: '2', name: '已结束', type: 'info' },
      { id: '3', name: '已结束', type: 'info' },
      { id: '4', name: '待补款', type: 'warning' },
      { id: '5', name: '已冻结', type: 'info' }
    ],
    finishStatus: [
      { label: '提前完成', value: '1' },
      { label: '超时完成', value: '2' },
      { label: '按时完成', value: '3' }
    ],
    registerType: [
      { value: '1', label: '本片' },
      { value: '2', label: '本区外片' },
      { value: '3', label: '本市外区' },
      { value: '4', label: '外籍' },
      { value: '5', label: '港澳台' }
    ],
    finPlanType: [
      { id: '1', name: '启用', value: false },
      { id: '2', name: '禁用', value: true }
    ]
  }
}

const mutations = {
  SET_DICT: (state, payload) => {
    state.dict = {
      ...state.dict,
      ...payload
    }
  }
}

const actions = {
  async getDict (
    { commit, state },
    { fieldName, fn = getDict, params, data, prop, cb }
  ) {
    console.log(fieldName)
    if (state[fieldName]) {
      return state[fieldName]
    }
    if (fieldName === 'roleTypes') {
      fn = getRoleTypes
      params = {}
    }
    const { label = 'label', value = 'val' } = prop || {}
    const res = await fn(params || { type: fieldName }, data || {})
    const list = cb
      ? cb(res)
      : (Array.isArray(res) ? res : res.content || []).map((x) => {
        x.label = x[label]
        x.value = x[value]
        return x
      })
    commit('SET_DICT', { [fieldName]: list })
    return list || []
  },
  async setDict ({ dispatch }, { type, list }) {
    const setData = Array.isArray(list) ? list : Array.of(list)
    const promises = []
    setData.forEach((x) => {
      promises.push(
        new Promise((resolve) => {
          dictDataSubmit({ type, label: x.label, val: x.val }).then(() => {
            resolve()
          })
        })
      )
    })
    Promise.all(promises).then(() => {
      dispatch('getDict', { fieldName: type })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
